/**
 * 防抖函数
 * @param fn
 * @param wait
 * @returns {(function(): void)|*}
 */
export const debounce = (fn, wait) => {
  let timer = null;

  return function () {
    let context = this,
      args = arguments;

    if (timer) {
      clearTimeout(timer);
      timer = null;
    }

    timer = setTimeout(() => {
      fn.apply(context, args);
    }, wait);
  };
};
/**
 * 节流函数
 * @param fn
 * @param delay
 * @returns {(function(): (*|undefined))|*}
 */
export const throttle = (fn, delay) => {
  let curTime = Date.now();

  return function () {
    let context = this,
      args = arguments,
      nowTime = Date.now();

    if (nowTime - curTime >= delay) {
      curTime = Date.now();
      return fn.apply(context, args);
    }
  };
};
export default {
  debounce,
  throttle,
};
